package com.redissession.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/session")
public class SessionController {

    // 写入 Session
    @GetMapping("/set")
    public String setSession(HttpSession session) {
        session.setAttribute("user", "admin");  // 存储用户信息
        session.setAttribute("token", "abc123");  // 存储 token
        return "Session 已存储，SessionID=" + session.getId();
    }

    // 读取 Session
    @GetMapping("/get")
    public String getSession(HttpSession session) {
        String user = (String) session.getAttribute("user");
        String token = (String) session.getAttribute("token");
        return "当前用户: " + user + ", Token: " + token + ", SessionID=" + session.getId();
    }

    // 销毁 Session（登出）
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();  // 使 Session 失效
        return "Session 已销毁";
    }
}